package com.delu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.entity.Coupon;
import com.delu.entity.CouponType;
import com.delu.entity.StorageTable;
import com.delu.mapper.CouponTypeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-19
 */
@RestController
@Api("优惠券类型服务接口")
@RequestMapping("/couponTypeController")
@RefreshScope //开启自动刷新配置
public class CouponTypeController {

    @Autowired
    private CouponTypeMapper couponTypeMapper;

    @PostMapping
    @ApiOperation("优惠券类型增加方法")
    //paramType:body 表示@RequestBody接受的实体类 path:表示路径/{} 传参  query @RequestParam接收参数
    @ApiImplicitParam(name = "couponType",value = "优惠券类型实体类",required = true,paramType = "body")
    public Result insertCoupon(@RequestBody CouponType couponType) {
        try {
            CouponType couponType1 = JSONObject.parseObject(JSON.toJSONString(couponType), CouponType.class);
            couponTypeMapper.insert(couponType1);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @PutMapping//@RequestParam java中的数据类型如string 加上此注解表名参数为必填项
    //@RequestBody  就收实体类要求前台必须用json的格式传参
    @ApiOperation(value = "优惠券类型实体类修改")
    @ApiImplicitParam(name = "couponType",value = "优惠券类型实体类",required = true,paramType = "path")
    public Result updateCouponById(@RequestBody CouponType couponType) {
        try {
            CouponType couponType1 = JSONObject.parseObject(JSON.toJSONString(couponType), CouponType.class);
            couponTypeMapper.updateById(couponType1);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @GetMapping
    @ApiOperation(value = "根据优惠类型券id查询")
    @ApiImplicitParam(name = "couponType",value = "优惠券类型id",required = true,paramType = "query")
    public Result selectCouponById(@RequestParam("couponType") Integer couponId) {
        try {
            CouponType couponType = couponTypeMapper.selectById(couponId);
            return Result.response(ResultEnum.SUCCESS).setData(couponType);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }



}
